<?php
error_reporting(0);
define("API_KEY", "在这里替换你的信息");
define('SECRET_KEY', '在这里替换你的信息');
/**
 * 发起http post请求(REST API), 并获取REST请求的结果
 * @param string $url
 * @param string $param
 * @return - http response body if succeeds, else false.
 */
function request_post($url = '', $param = '')
{
    if (empty($url) || empty($param)) {
        return false;
    }

    $postUrl = $url;
    $curlPost = $param;
    // 初始化curl
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $postUrl);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 要求结果为字符串且输出到屏幕上
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    // post提交方式
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    // 运行curl
    $data = curl_exec($curl);
    curl_close($curl);

    return $data;
}
/**
 * 发送请求，获取access_token
 * @param null
 * @return - return array with token server return;
 */
function get_token()
{
    $url = 'https://aip.baidubce.com/oauth/2.0/token';
    $post_data['grant_type']       = 'client_credentials';
    $post_data['client_id']      = API_KEY;
    $post_data['client_secret'] = SECRET_KEY;
    $o = "";
    foreach ($post_data as $k => $v) {
        $o .= "$k=" . urlencode($v) . "&";
    }
    $post_data = substr($o, 0, -1);
    $res = request_post($url, $post_data);
    return json_decode($res, true);
    // var_dump($res);
}

/**
 * 发送请求，返回识别结果
 * @param string $filepath 图片路径 
 * @param bool $in_clued   是否为相对路径 是的话为 true
 * @return - return array with result if succeeds, else false.
 */

// function get_res_file($filepath, $in_clued)
// {
//     $get_token = get_token();
//     if (!array_key_exists("access_token", $get_token)) {
//         $res['code'] = 502;
//         $res['msg'] = "Token已失效！";
//         return $res;
//     }
//     $img = file_get_contents($filepath, $in_clued);
//     $token = $get_token['access_token'];
//     $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=' . $token;
//     // 判断文件是否存在
//     if (!file_exists($filepath)) {
//         $res['code'] = 404;
//         $res['msg'] = "文件不存在！";
//         return $res;
//     }

//     $img = base64_encode($img);
//     // print_r($img);
//     $bodys = array(
//         'image' => $img,
//         "language_type" => "CHN_ENG",
//         'detect_direction' => "true"
//     );
//     $req_res = request_post($url, $bodys);
//     $result = json_decode($req_res, true);
//     // 判断返回信息中是否包含error字段
//     if (array_key_exists("error_msg", $result)) {
//         $res['code'] = 500;
//         $res['msg'] = $result['error_msg'];
//         return $res;
//     }
//     $res['code'] = 200;
//     $res['meta'] = $result['words_result'];
//     return $res;
// }
function get_res_base64($img)
{
    $get_token = get_token();
    if (!array_key_exists("access_token", $get_token)) {
        $res['code'] = 502;
        $res['msg'] = "Token已失效！";
        return $res;
    }
    $token = $get_token['access_token'];
    $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=' . $token;

    $bodys = array(
        'image' => $img,
        "language_type" => "CHN_ENG",
        'detect_direction' => "true"
    );
    $req_res = request_post($url, $bodys);
    $result = json_decode($req_res, true);
    // 判断返回信息中是否包含error字段
    if (array_key_exists("error_msg", $result)) {
        $res['code'] = 500;
        $res['msg'] = $result['error_msg'];
        return $res;
    }
    if ($result['words_result_num'] == 0) {
        $res['code'] = 202;
        $res['msg'] = "未检测到文字。";
        return $res;
    }
    $res['code'] = 200;
    $res['meta'] = $result['words_result'];
    return $res;
}

$basedata = $_REQUEST['basecode'];
$res = get_res_base64($basedata);
echo json_encode($res);
